<template>
    <el-container>
        <el-container class="is-vertical">
            <div class="panel-search-box">
                <div class="panel-search">
                    <el-date-picker v-model="search.date" value-format="YYYY-MM-DD" type="date" placeholder="选择日期">
                    </el-date-picker>
                    <el-input v-model="search.userName" placeholder="学员姓名" clearable></el-input>
                    <el-input v-model="search.agentName" placeholder="机构" clearable></el-input>
                    <el-select v-model="search.type" placeholder="类型" clearable>
                        <el-option label="充值" :value="1"></el-option>
                        <el-option label="下级充值" :value="2"></el-option>
                        <el-option label="学员充值" :value="9"></el-option>
                        <el-option label="线上充值" :value="12"></el-option>
                        <el-option label="退费" :value="5"></el-option>
                        <el-option label="下级退费" :value="10"></el-option>
                        <el-option label="学员退费" :value="11"></el-option>
                        <el-option label="开户" :value="3"></el-option>
                        <el-option label="开户失败" :value="4"></el-option>
                        <el-option label="上课" :value="6"></el-option>
                        <el-option label="兑换" :value="7"></el-option>
                        <el-option label="兑换扣除" :value="8"></el-option>
                    </el-select>
                    <el-select v-model="search.classHourType" placeholder="课时类型" clearable>
                        <el-option label="30分钟" :value="1"></el-option>
                        <el-option label="60分钟" :value="2"></el-option>
                        <el-option label="体验课" :value="4"></el-option>
                    </el-select>
                    <el-select v-model="search.paymentMethod" placeholder="付款方式" clearable>
                        <el-option label="支付宝" :value="1"></el-option>
                        <el-option label="微信" :value="2"></el-option>
                    </el-select>
                    <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                    <el-button type="primary" icon="el-icon-download" @click="exportHander">导出</el-button>
                </div>
            </div>
            <el-main class="nopadding">
                <scTable ref="table" :apiObj="$API.classHour.log.list" remoteSort :params="search" @selection-change="selectionChange" @dataChange="dataChange" :exportData="exportData" :formatExportData="formatExportData">
                    <el-table-column label="序号" prop="index" width="70" align="left"></el-table-column>
                    <el-table-column label="来源" prop="originAgent" width="160" align="center">
                        <template #default="scope">
                            {{ scope.row.originAgent ? scope.row.originAgent.name : '' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="机构名称" prop="agent" align="center" width="160">
                        <template #default="scope">
                            {{ scope.row.agent ? scope.row.agent.name : '' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="学员姓名" prop="user" width="150" align="center">
                        <template #default="scope">
                            {{ scope.row.user ? scope.row.user.name : '' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="类型" prop="type" width="160" align="center">
                        <template #default="scope">
                            {{ scope.row.type ? scope.row.type.name : '' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="课时类型" prop="classHourType" width="120" align="center">
                        <template #default="scope">
                             {{ scope.row.classHourType ? scope.row.classHourType.name : '' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="数量" prop="classHour" width="120" align="center">
                        <template #default="scope">
                            <template v-if="in_array(scope.row.type.value, [1,4,7,10,11])">
                                <div style="color: green;">+ {{ scope.row.classHour }}</div>
                            </template>
                            <template v-else>
                                <div style="color: red;">- {{ scope.row.classHour }}</div>
                            </template>
                        </template>
                    </el-table-column>
                    <el-table-column label="变更前数量" prop="beforeNumber" width="120" align="center">
                    </el-table-column>
                    <el-table-column label="变更后数量" prop="afterNumber" width="120" align="center">
                    </el-table-column>
                    <el-table-column label="金额" prop="amount" width="120" align="center">
                    </el-table-column>
                    <el-table-column label="付款方式" prop="paymentMethod" width="120" align="center">
                        <template #default="scope">
                             {{ scope.row.paymentMethod ? scope.row.paymentMethod.name : '0' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="描述" prop="description" align="center" width="150" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column label="操作者" prop="operator" width="160" align="center">
                        <template #default="scope">
                             {{ scope.row.operator ? scope.row.operator.name : '' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="操作时间" prop="createdAt" width="150" align="center"></el-table-column>
                </scTable>
            </el-main>
        </el-container>
    </el-container>

</template>

<script>
import { in_array } from '@/utils/index.js'
export default {
    components: {
    },
    data() {
        return {
            in_array,
            selection: [],
            search: {
                name: null,
            },
            exportData: {
                header: ['来源', '机构名称', '学员姓名', '教练', '类型', '课时类型', '数量', '变更前数量', '变更后数量', '金额', '付款方式', '描述', '操作者', '操作时间'],
                column: ['originAgent', 'agentName', 'userName', 'teacherName', 'type', 'classHourType', 'classHour', 'beforeNumber', 'afterNumber', 'amount', 'paymentMethod', 'description', 'operator', 'createdAt'],
                filename: '课时记录列表',
                formatData: true
            },
        }
    },
    mounted() {
       
    },
    methods: {
        dataChange(res, tableData) {
            let perPage = res.data.perPage
            let page = (this.$refs.table && this.$refs.table.currentPage) || 1
            tableData.forEach((item, index) => {
				item.index = page * perPage + index - perPage + 1
            })
        },
        // 搜索
        upsearch() {
            this.$refs.table.upData(this.search)
        },
        exportHander() {
			this.$refs.table.exportHander(this.search)
		},
		formatExportData(item) {
            item.originAgent = item.originAgent ? item.originAgent.name : ''
            item.agentName = item.agent ? item.agent.name : ''
            item.userName = item.user ? item.user.name : ''
            if (in_array(item.type.value, [1,4,7,10,11])) {
                item.classHour = '+' + item.classHour
            }else{
                item.classHour = '-' + item.classHour
            }
            item.classHourType = item.classHourType ? item.classHourType.name : ''
            item.type = item.type ? item.type.name : ''
            item.paymentMethod = item.paymentMethod ? item.paymentMethod.name : ''
            item.operator = item.operator ? item.operator.name : ''
            item.teacherName = item.teacher ? item.teacher.name : ''
            return item
        },
        //表格选择后回调事件
        selectionChange(selection) {
            this.selection = selection;
        },
        //本地更新数据
        handleSuccess() {
            this.$refs.table.refresh()
        }
    }
}
</script>

<style>
</style>
